home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / mslang / sktdem / simpsktc.c_ / simpsktc.c
Encoding:
C/C++ Source or Header  |  1994-02-14  |  4.4 KB  |  197 lines

  1. /*  Simple Sockets Toolkit Demo.
  2.  *  Copyright 1994 Intelec Systems Corportion, APIary.
  3.  *  This demonstration allows you to send and receive
  4.  *  text messages between two machines.
  5.  *      syntax: SIMPSKTC <Local Name> <Remote Name>
  6.  *
  7.  *
  8.  *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  9.  *  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10.  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  11.  *  PURPOSE.
  12.  */
  13.  
  14.  
  15. #include <skt.h>
  16. #include <mem.h>
  17. #include <string.h>
  18.  
  19. long FAR PASCAL WndProc( HWND , UINT , WPARAM , LPARAM );
  20. BOOL CALLBACK DlgProc( HWND , UINT , WPARAM , LPARAM );
  21.  
  22. HINSTANCE hInst;
  23. BYTE Msg[ 128 ];
  24. BYTE LocalName[ 17 ];
  25. BYTE RemoteName[ 17 ];
  26. DWORD hSocket;
  27.  
  28.  
  29. int PASCAL WinMain ( HANDLE hInstance ,
  30.              HANDLE hPrevInstance ,
  31.              LPSTR lpszCmdLine ,
  32.              int nCmdShow )
  33. {
  34.       WNDCLASS wndclass;
  35.       HWND hwnd;
  36.       MSG msg;
  37.  
  38.       if( sscanf( lpszCmdLine , "%s %s" , LocalName , RemoteName ) < 2 )
  39.           {
  40.               MessageBox( GetFocus() ,
  41.                   "syntax:\x0dSIMPSKTC <LocalName> <RemoteName>" ,
  42.                   "Simple Socket Demo" ,
  43.                   MB_ICONSTOP );
  44.               return 1;
  45.           }
  46.  
  47.       if (!hPrevInstance)
  48.       {
  49.            wndclass.style     = CS_HREDRAW | CS_VREDRAW;
  50.            wndclass.lpfnWndProc    = WndProc;
  51.            wndclass.cbClsExtra     = 0;
  52.            wndclass.cbWndExtra     = 0;
  53.            wndclass.hInstance      = hInstance;
  54.            wndclass.hIcon          = LoadIcon(hInstance, "ICON_1");
  55.            wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  56.            wndclass.hbrBackground  = COLOR_WINDOW + 1;
  57.            wndclass.lpszMenuName   = "MAIN_MENU";
  58.            wndclass.lpszClassName  = "SimpSktC";
  59.  
  60.            if (!RegisterClass (&wndclass))
  61.               return FALSE ;
  62.       }
  63.  
  64.  
  65.       hInst = hInstance;
  66.  
  67.       hwnd = CreateWindow ( "SimpSktC",
  68.                 "Simple Socket Demo",
  69.                  WS_OVERLAPPEDWINDOW,
  70.                  CW_USEDEFAULT,
  71.                  CW_USEDEFAULT,
  72.                  CW_USEDEFAULT,
  73.                  CW_USEDEFAULT,
  74.                  NULL,
  75.                  NULL,
  76.                  hInstance,
  77.                  NULL );
  78.       ShowWindow( hwnd , nCmdShow );
  79.       UpdateWindow( hwnd );
  80.  
  81.       while ( GetMessage( &msg , NULL , 0 , 0 ) )
  82.       {
  83.          TranslateMessage( &msg ) ;
  84.          DispatchMessage( &msg ) ;
  85.       }
  86.  
  87.       return msg.wParam ;
  88. }
  89.  
  90. long FAR PASCAL WndProc( HWND hWnd,
  91.              UINT iMessage ,
  92.              WPARAM wParam ,
  93.              LPARAM lParam )
  94. {
  95.      switch ( iMessage )
  96.      {
  97.          case WM_CREATE:
  98.            if( SktOpen( LocalName , 0 , hWnd , 128 ,
  99.                 1 , 1 , 0 , &hSocket ) )
  100.             {
  101.                 MessageBox( GetFocus() ,
  102.                     "Unable to open socket!" ,
  103.                     "Simple Socket Demo" ,
  104.                     MB_ICONSTOP );
  105.                 return -1;
  106.             }
  107.            SktSetTarget( hSocket , RemoteName , 0 , NULL );
  108.            break;
  109.  
  110.          case WM_SKTEVENT:
  111.             switch( wParam )
  112.                {
  113.                    case SEV_READ:
  114.                     {
  115.                     LPSKTEVENT Evt = ( LPSKTEVENT ) lParam;
  116.                     _fmemset( Msg , '\0' , sizeof( Msg ) );
  117.                     _fmemcpy( Msg , Evt->lpBuffer , Evt->dwLength );
  118.                     MessageBox( hWnd ,
  119.                             Msg ,
  120.                             "Socket Event Read" ,
  121.                             MB_ICONEXCLAMATION );
  122.                     return 0L;
  123.                      }
  124.                    case SEV_WRITE:
  125.                      MessageBox( hWnd ,
  126.                          "Message sent!" ,
  127.                          "Socket Event Write" ,
  128.                          MB_ICONEXCLAMATION );
  129.                      return 0L;
  130.                }
  131.          case WM_DESTROY:
  132.              SktClose( hSocket );
  133.              PostQuitMessage( 0 );
  134.              break;
  135.  
  136.          case WM_COMMAND:
  137.             switch( wParam )
  138.             {
  139.                 // Send
  140.                 case 1:
  141.                  {
  142.                   DLGPROC dlgproc;
  143.                   int Ret;
  144.                   dlgproc = MakeProcInstance( DlgProc , hInst );
  145.                   Ret = DialogBox( hInst , "SEND_DLG" , hWnd , dlgproc );
  146.                   FreeProcInstance( dlgproc );
  147.                   if( Ret )
  148.                      {
  149.                        if( hSocket )
  150.                            {
  151.                               SktWrite( hSocket ,
  152.                                 Msg ,
  153.                                 strlen( Msg ),
  154.                                 0 );
  155.                            }
  156.                      }
  157.                   break;
  158.                  }
  159.  
  160.                 // Exit
  161.                 case 2:
  162.                   PostMessage( hWnd , WM_CLOSE , 0 , 0 );
  163.             }
  164.             break;
  165.      }
  166.      return DefWindowProc ( hWnd , iMessage , wParam , lParam ) ;
  167. }
  168.  
  169. BOOL CALLBACK DlgProc( HWND hWnd,
  170.                UINT iMessage ,
  171.                WPARAM wParam ,
  172.                LPARAM lParam )
  173. {
  174.      switch ( iMessage )
  175.      {
  176.          case WM_INITDIALOG:
  177.               return TRUE;
  178.          case WM_COMMAND:
  179.             switch( wParam )
  180.               {
  181.                  case IDOK:
  182.                     SendDlgItemMessage( hWnd , 101 ,
  183.                                 WM_GETTEXT ,
  184.                                 sizeof( Msg ) ,
  185.                                 ( LPARAM ) Msg );
  186.                     EndDialog( hWnd , TRUE );
  187.                     break;
  188.                  case IDCANCEL:
  189.                     EndDialog( hWnd , FALSE );
  190.               }
  191.               break;
  192.      }
  193.      return FALSE;
  194. }
  195.  
  196.  
  197.